package CCNQ::Billing::Info;
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
use strict; use warnings;

use base qw(CCNQ::Object);

use configuration;

sub _init
{
    my $self = shift;
    $self->_init_billto();
    $ENV{TZ} = defined($configuration::bill_timezone) ? $configuration::bill_timezone : 'UTC';
}

sub default_src($)
{
    my $self = shift;
    return $self->{default_src}->{$_[0]}
        if exists $self->{default_src}->{$_[0]} and defined $self->{default_src}->{$_[0]};
    return $configuration::default_src;
}

sub billto($)    {   my $self = shift; return $self->{billto}->{$_[0]} }
sub billto_as_text 
{ 
    my $self = shift;
    return 
        join('', map { 
                join("\t", $_, $self->{billto}->{$_}||'' )."\n"
            } sort keys %{$self->{billto}}
        );
}

sub _init_billto()
{
    my $self = shift;

    my $bill_servers =
        defined $configuration::bill_servers 
            ? $configuration::bill_servers
            : [ { 
                host => $configuration::db_host, 
                name => $configuration::db_name, 
                login => $configuration::db_login, 
                password => $configuration::db_password
                } ];

    for my $bill_server (@{$bill_servers})
    {
        # Gather the records using LWP
        if(exists $bill_server->{uri})
        {
            use LWP;
            my $ua = LWP::UserAgent->new;
            $ua->credentials( $bill_server->{location}, $bill_server->{realm},
                $bill_server->{username}, $bill_server->{password} );
            my $response = $ua->get($bill_server->{uri});
            if($response->is_success)
            {
                for my $line (split(/\n/,$response->content))
                {
                    my @row = split(/\t/,$line);
                    $self->{billto}->{$row[0]} = $row[1];
                }
            }
            else
            {
                die $response->status_line;
            }
            undef $ua;
            next;
        }

        # Gather the records using DBI
        if(exists $bill_server->{name})
        {
            use DBI;
            our $dbi_info =
                'DBI:mysql:database='.$bill_server->{name}.
                ';host='.$bill_server->{host};
            my $db  = DBI->connect ($dbi_info,$bill_server->{login},$bill_server->{password})
              or die $DBI::errstr;
        
            my $sth = $db->prepare(<<'SQL');
                SELECT username, account FROM subscriber
SQL
            $sth->execute();
            while( my $row = $sth->fetchrow_arrayref() )
            {
                $self->{billto}->{$row->[0]} = $row->[1];
                # $self->{default_src}->{$row->[0]} = $row->[2];
            }
            undef $sth;
            $db->disconnect();
            undef $db;
            next;
        }

        # None of the above
        die "Invalid options: ".%{$bill_server};
    }
}

1;